package com.coderman.infosys.auth.infrast.repositoryimpl;

import com.coderman.infosys.auth.domain.bo.role.RoleBO;
import com.coderman.infosys.auth.domain.bo.user.UserGroupBO;
import com.coderman.infosys.auth.domain.bo.role.RoleGroupBO;
import com.coderman.infosys.auth.domain.repository.UserRoleRepository;
import com.coderman.infosys.auth.infrast.cache.UserRoleGroupCache;
import com.coderman.infosys.auth.infrast.dao.mapper.RoleUserMapper;
import com.coderman.infosys.auth.infrast.dataconvert.UserGroupConvert;
import com.coderman.infosys.auth.infrast.dao.mapper.RoleMapper;
import com.coderman.infosys.auth.infrast.dao.mapper.UserGroupMapper;
import com.coderman.infosys.auth.infrast.dao.mapper.RoleGroupMapper;
import com.coderman.infosys.auth.infrast.dao.dataobject.RoleGroupDO;
import com.coderman.infosys.auth.infrast.mq.producer.UserGroupMqProducer;
import java.util.List;
import com.coderman.infosys.auth.infrast.dataconvert.RoleConvert;
import com.coderman.infosys.auth.infrast.dataconvert.RoleGroupConvert;
import com.coderman.infosys.auth.infrast.dao.dataobject.RoleDO;
import com.coderman.infosys.auth.infrast.dao.dataobject.UserGroupDO;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * @Description:用户和角色相关接口实现类
 * @Author：
 * @CreateTime：2022-01-02 15:27:38
 * @version v1.0
 */
@Service
public class UserRoleRepositoryImpl  implements UserRoleRepository{

	private  Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private RoleGroupMapper roleGroupMapper;

    @Autowired
    private UserGroupMapper userGroupMapper;


    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleUserMapper roleUserMapper;

    @Override
	public Long registUserGroup(UserGroupBO userGroupBO){
        UserGroupDO userGroupDO = UserGroupConvert.INSTANCE.bo2do(userGroupBO);
        userGroupMapper.insert(userGroupDO);
        return userGroupDO.getId();
    }

    @Override
	public Integer updateUserGroup(UserGroupBO userGroupBO){
        UserGroupDO userGroupDO = UserGroupConvert.INSTANCE.bo2do(userGroupBO);
        int intVar = userGroupMapper.update(userGroupDO);
        return intVar;
    }

    @Override
	public Long saveRoleGroup(RoleGroupBO roleGroupBO){
        RoleGroupDO roleGroupDO = RoleGroupConvert.INSTANCE.bo2do(roleGroupBO);

        roleGroupMapper.insert(roleGroupDO);
        return roleGroupDO.getId();
    }

    @Override
	public Integer updateRoleGroup(RoleGroupBO roleGroupBO){
        RoleGroupDO roleGroupDO = RoleGroupConvert.INSTANCE.bo2do(roleGroupBO);
        int intVar = roleGroupMapper.update(roleGroupDO);
        return intVar;
    }

    @Override
	public Long saveRole(RoleBO roleBO){
        RoleDO roleDO = RoleConvert.INSTANCE.bo2do(roleBO);
       /* if(CollectionUtils.isNotEmpty(roleBO.getRoleUserBOList())){
            //todo convert insert
        }*/
        if(CollectionUtils.isNotEmpty(roleBO.getUserList())){
            //todo convert insert
        }
        roleMapper.insert(roleDO);
        return roleDO.getId();
    }

    @Override
	public Integer updateRole(RoleBO roleBO){
        RoleDO roleDO = RoleConvert.INSTANCE.bo2do(roleBO);

        /* if(CollectionUtils.isNotEmpty(roleBO.getRoleUserBOList())){
            //todo remove RoleUserRelationDO List
            roleUserMapper.removeRelation()
            //todo convert insert
        }*/
        if(CollectionUtils.isNotEmpty(roleBO.getUserList())){
            //todo remove RoleUserRelationDO List
            //todo convert insert
        }

        int intVar = roleMapper.update(roleDO);
        return intVar;
    }

    @Override
	public UserGroupBO getByUserGroupCode(String userGroupCode){
        UserGroupDO userGroupDO = userGroupMapper.getByUserGroupCode(userGroupCode);
        return UserGroupConvert.INSTANCE.do2bo(userGroupDO);
    }

    @Override
	public RoleBO getByRoleCode(String roleCode){
        RoleDO roleDO = roleMapper.getByRoleCode(roleCode);


        return RoleConvert.INSTANCE.do2bo(roleDO);
    }

    @Override
	public RoleGroupBO getByRoleGroupCode(String roleGroupCode){
        RoleGroupDO roleGroupDO = roleGroupMapper.getByRoleGroupCode(roleGroupCode);
        return RoleGroupConvert.INSTANCE.do2bo(roleGroupDO);
    }

    @Override
    public Boolean disableRole(String roleCode) {
        RoleBO roleBO = RoleConvert.INSTANCE.do2bo(roleMapper.getByRoleCode(roleCode));
        if(roleBO == null){
            return false;
        }
        roleBO.disable();
        int updateCount = roleMapper.update(RoleConvert.INSTANCE.bo2do(roleBO));
        return updateCount == 1;
    }

    @Override
    public Boolean enableRole(String roleCode) {
        RoleBO roleBO = RoleConvert.INSTANCE.do2bo(roleMapper.getByRoleCode(roleCode));
        if(roleBO == null){
            return false;
        }
        roleBO.enable();
        int updateCount = roleMapper.update(RoleConvert.INSTANCE.bo2do(roleBO));
        return updateCount == 1;
    }

    @Override
    public Boolean disableUserGroup(String userGroupCode) {
        UserGroupBO userGroupBO = UserGroupConvert.INSTANCE.do2bo(userGroupMapper.getByUserGroupCode(userGroupCode));
        if(userGroupBO == null){
            return false;
        }
        userGroupBO.disable();
        int updateCount = userGroupMapper.update(UserGroupConvert.INSTANCE.bo2do(userGroupBO));
        return updateCount == 1;
    }

    @Override
    public Boolean enableRoleGroup(String roleGroupCode) {
        RoleGroupBO roleGroupBO = RoleGroupConvert.INSTANCE.do2bo(roleGroupMapper.getByRoleGroupCode(roleGroupCode));
        roleGroupBO.enable();
        int updateCount = roleGroupMapper.update(RoleGroupConvert.INSTANCE.bo2do(roleGroupBO));
        return updateCount == 1;
    }

    @Override
    public Boolean disableRoleGroup(String roleGroupCode) {
        RoleGroupBO roleGroupBO = RoleGroupConvert.INSTANCE.do2bo(roleGroupMapper.getByRoleGroupCode(roleGroupCode));
        if(roleGroupBO == null){
            return false;
        }
        roleGroupBO.disable();
        int updateCount = roleGroupMapper.update(RoleGroupConvert.INSTANCE.bo2do(roleGroupBO));
        return updateCount == 1;
    }

    @Override
    public Boolean enableUserGroup(String userGroupCode) {
        UserGroupBO userGroupBO = UserGroupConvert.INSTANCE.do2bo(userGroupMapper.getByUserGroupCode(userGroupCode));
        if(userGroupBO == null){
            return false;
        }
        userGroupBO.enable();
        int updateCount = userGroupMapper.update(UserGroupConvert.INSTANCE.bo2do(userGroupBO));
        return updateCount == 1;
    }

    @Override
    public Boolean removeUser(Long userId) {

        return null;
    }

}